<template>
  <el-dialog :visible.sync="currentShowFlag" append-to-body fullscreen :close-on-click-modal="false" :close-on-press-escape="false" @open="handleDialogOpen" @closed="handleDialogClosed">
    <div slot="title" class="detail-dialog__header">多种经营业务信息收集表</div>
    <div>
      <el-tabs v-model="activeName" stretch>
        <el-tab-pane label="基本情况" name="first">
          <simple-form ref="first" :validate-info="{tabName: 'first' }" v-model="formModel" label-position="top" :form-field="formField.first" :grid="{xs: 12, sm: 12, md: 8}" :view-flag="viewFlag"></simple-form>
        </el-tab-pane>
        <el-tab-pane label="改革推进情况" name="second">
          <simple-form ref="second" :validate-info="{tabName: 'second' }" v-model="formModel" label-position="top" label-width="100px" :form-field="formField.second" :grid="{xs: 12, sm: 12, md: 8}" :view-flag="viewFlag"></simple-form>
        </el-tab-pane>
        <el-tab-pane label="资产负债情况" name="third">
          <simple-form-container title="期末数">
            <simple-form ref="third" :validate-info="{tabName: 'third' }" v-model="formModel" label-position="top" label-width="100px" :form-field="formField.third" :grid="{xs: 12, sm: 12, md: 8}" :view-flag="viewFlag">
            </simple-form>
          </simple-form-container>
        </el-tab-pane>
        <el-tab-pane label="主要经营情况" name="fourth">
          <simple-form ref="fourth_1" :validate-info="{tabName: 'fourth' }" v-model="formModel" label-position="top" :form-field="formField.fourth_1">
          </simple-form>
          <simple-form-container title="营业收入">
            <simple-form ref="fourth_2" :validate-info="{tabName: 'fourth' }" v-model="formModel" label-position="top" :form-field="formField.fourth_2" :grid="{xs: 12, sm: 12, md: 8}" :view-flag="viewFlag">
            </simple-form>
          </simple-form-container>
          <simple-form-container title="成本费用">
            <simple-form ref="fourth_3" :validate-info="{tabName: 'fourth' }" v-model="formModel" label-position="top" :form-field="formField.fourth_3" :grid="{xs: 12, sm: 12, md: 8}" :view-flag="viewFlag">
            </simple-form>
          </simple-form-container>
          <simple-form ref="fourth_4" :validate-info="{tabName: 'fourth' }" v-model="formModel" label-position="top" :form-field="formField.fourth_4" :grid="{xs: 12, sm: 12, md: 8}" :view-flag="viewFlag">
          </simple-form>
        </el-tab-pane>
        <el-tab-pane label="在职人员情况" name="fifth">
          <simple-form ref="fifth_1" :validate-info="{tabName: 'fifth' }" v-model="formModel" label-position="top" :form-field="formField.fifth_1">
          </simple-form>
          <simple-form-container title="减员">
            <simple-form ref="fifth_2" :validate-info="{tabName: 'fifth' }" v-model="formModel" label-position="top" label-width="100px" :form-field="formField.fifth_2" :grid="{xs: 12, sm: 12, md: 8}" :view-flag="viewFlag">
            </simple-form>
          </simple-form-container>
          <simple-form-container title="增员">
            <simple-form ref="fifth_3" :validate-info="{tabName: 'fifth' }" v-model="formModel" label-position="top" label-width="100px" :form-field="formField.fifth_3" :grid="{xs: 12, sm: 12, md: 8}" :view-flag="viewFlag">
            </simple-form>
          </simple-form-container>
          <simple-form ref="fifth_4" :validate-info="{tabName: 'fifth' }" v-model="formModel" label-position="top" :form-field="formField.fifth_4" :view-flag="viewFlag"></simple-form>
          <simple-form ref="fifth_5" :validate-info="{tabName: 'fifth' }" v-model="formModel" label-position="top" :form-field="formField.fifth_5" :view-flag="viewFlag"></simple-form>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div slot="footer">
      <el-button @click="handleDialogClose">{{viewFlag ? $t('operation.close') : $t('operation.cancel')}}</el-button>
      <el-button type="primary" @click="handleDataSubmit" v-if="!viewFlag">{{$t('operation.submit')}}</el-button>
    </div>
  </el-dialog>
</template>
<script>
const BASEURL = {
  save: '/Web/BizController/ajax.html?sys_method=Add&sys_objName=Biz_Diversified_Business',
  update: '/Web/BizController/ajax.html?sys_method=Update&sys_objName=Biz_Diversified_Business'
}
export default {
  name: 'balance_diversification_detail',
  data () {
    return {
      activeName: 'first',
      formModel: {}
    }
  },
  inject: ['handleDataSearch'],
  computed: {
    currentShowFlag: {
      get () { return this.showFlag },
      set (val) { this.$emit('update:showFlag', val) }
    },

    formField () {
      return {
        first: [
          {
            type: 'Select',
            prop: 'companyname',
            label: '企业名称',
            component: { optionList: this.extData.orgList.map(item => { return { key: item.value, value: item.value, text: item.text } }) },
            rules: { trigger: 'change', message: this.$t('validate.notSelect'), required: true }
          },
          {
            type: 'Datepicker',
            prop: 'uploadyear',
            label: '年份',
            component: { type: 'year', valueFormat: 'yyyy' },
            rules: { trigger: 'change', message: this.$t('validate.notSelect'), required: true }
          },
          {
            type: 'Select',
            prop: 'uploadquarter',
            label: '季度',
            component: { optionList: this.$getDictList('季度') },
            rules: { trigger: 'change', message: this.$t('validate.notSelect'), required: true }
          },
          {
            type: 'Input',
            prop: 'managementlevel',
            label: '管理层级',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true }
          },
          {
            type: 'Select',
            prop: 'propertytype',
            label: '产权性质',
            component: { optionList: this.$getDictList('产权性质_多种经营') },
            rules: { trigger: 'change', message: this.$t('validate.notSelect'), required: true }
          },
          {
            type: 'Input',
            prop: 'equitystructure',
            label: '股权结构',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true }
          },
          {
            type: 'Select',
            prop: 'businessstatus',
            label: '经营状态',
            component: { optionList: this.$getDictList('经营状态_多种经营') },
            rules: { trigger: 'change', message: this.$t('validate.notSelect'), required: true }
          },
          {
            type: 'Datepicker',
            prop: 'plannedcompletiontime',
            label: '计划完成时间',
            component: { type: 'date', valueFormat: 'yyyy-MM-dd' },
            rules: { trigger: 'change', message: this.$t('validate.notSelect'), required: true }
          },
          {
            type: 'Select',
            prop: 'isbelongedtounlisted',
            label: '是否属于未上市托管企业',
            component: { optionList: this.$getDictList('是否') },
            rules: { trigger: 'change', message: this.$t('validate.notSelect'), required: true }
          },
          {
            type: 'Select',
            prop: 'businesstype',
            label: '业务类型',
            component: { optionList: this.$getDictList('业务类型') },
            rules: { trigger: 'change', message: this.$t('validate.notSelect'), required: true }
          }

        ],

        second: [
          {
            type: 'Select',
            prop: 'reformmethod',
            label: '改革方式',
            component: { optionList: this.$getDictList('改革方式_多种经营') },
            rules: { trigger: 'change', message: this.$t('validate.notSelect'), required: true }
          },
          {
            type: 'Input',
            prop: 'reformmeasuresprogress',
            label: '改革措施与进展',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true }
          },
          {
            type: 'Input',
            prop: 'existingproblem',
            label: '存在问题',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true }
          },
          {
            type: 'Input',
            prop: 'nextworkarrangement',
            label: '下一步工作安排',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true }
          }
        ],

        third: [
          {
            type: 'NumberInput',
            prop: 'totalassets',
            label: '资产总额',
            rules: [
              { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
              {
                trigger: 'blur',
                // message: '应大于(其中：货币资金+存货和应收账款净额+固定资产净额)',
                validator: (rule, value, callback) => {
                  if (isNaN(Number(this.formModel.currencyfund)) || isNaN(Number(this.formModel.stocknetreceivables)) || isNaN(Number(this.formModel.netvalueoffixedassets))) {
                    callback()
                  } else {
                    if (Number(value) >= (Number(this.formModel.currencyfund) +
                      Number(this.formModel.stocknetreceivables) +
                      Number(this.formModel.netvalueoffixedassets))) {
                      callback()
                    } else {
                      callback(new Error('应大于(其中：货币资金+存货和应收账款净额+固定资产净额)'))
                    }
                  }
                }
              }
            ],
            event: {
              change: () => { this.handleCalcAssetsliabilities() }
            }
          },
          {
            type: 'NumberInput',
            prop: 'currencyfund',
            label: '其中：货币资金',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: (val) => { if (val) this.$refs['third'].$refs.simpleForm.validateField('totalassets') }
            }
          },
          {
            type: 'NumberInput',
            prop: 'stocknetreceivables',
            label: '存货和应收账款净额',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: (val) => { if (val) this.$refs['third'].$refs.simpleForm.validateField('totalassets') }
            }
          },
          {
            type: 'NumberInput',
            prop: 'netvalueoffixedassets',
            label: '固定资产净额',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: (val) => { if (val) this.$refs['third'].$refs.simpleForm.validateField('totalassets') }
            }
          },
          {
            type: 'NumberInput',
            prop: 'totalliabilities',
            label: '负债总额',
            rules: [
              { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
              {
                trigger: 'blur',
                // message: '负债总额 >= 其中：带息负债金额',
                validator: (rule, value, callback) => {
                  if (isNaN(Number(this.formModel.debtwithinterest))) {
                    callback()
                  } else {
                    if (Number(value) >= Number(this.formModel.debtwithinterest)) {
                      callback()
                    } else {
                      callback(new Error('应大于(其中：带息负债金额)'))
                    }
                  }
                }
              }
            ],
            event: {
              change: () => { this.handleCalcAssetsliabilities() }
            }
          },
          {
            type: 'NumberInput',
            prop: 'debtwithinterest',
            label: '其中：带息负债金额',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: (val) => {
                if (val) this.$refs['third'].$refs.simpleForm.validateField('totalliabilities')
              }
            }
          },
          {
            type: 'NumberInput',
            prop: 'assetsliabilities',
            label: '资产负债率',
            viewFlag: true
          }
        ],

        fourth_1: [
          { type: 'Title', title: '本年累计', col: { span: 24 } }
        ],

        fourth_2: [
          {
            type: 'NumberInput',
            prop: 'totalincome',
            label: '收入总额',
            rules: [
              { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
              {
                trigger: 'blur',
                // message: '收入总额 >= 其中：来自主办单位的收入',
                validator: (rule, value, callback) => {
                  if (isNaN(Number(this.formModel.unitsponsoredincome))) {
                    callback()
                  } else {
                    if (Number(value) >= Number(this.formModel.unitsponsoredincome)) {
                      callback()
                    } else {
                      callback(new Error('应大于(其中：来自主办单位的收入)'))
                    }
                  }
                }
              }
            ],
            event: {
              change: () => { this.handleCalcTotalprofit() }
            }
          },
          {
            type: 'NumberInput',
            prop: 'unitsponsoredincome',
            label: '其中：来自主办单位的收入',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: (val) => {
                if (val) this.$refs['fourth_2'].$refs.simpleForm.validateField('totalincome')
              }
            }
          }
        ],

        fourth_3: [
          {
            type: 'NumberInput',
            prop: 'totalcost',
            label: '费用总额',
            rules: [
              { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
              {
                trigger: 'blur',
                // message: '应大于(其中：人工成本+利息支出+折旧折耗+资产减值损失)',
                validator: (rule, value, callback) => {
                  console.log(value)
                  if (isNaN(Number(this.formModel.laborcost)) ||
                    isNaN(Number(this.formModel.interestexpense)) ||
                    isNaN(Number(this.formModel.depreciationdepletion)) ||
                    isNaN(Number(this.formModel.assetimpairmentloss))) {
                    callback()
                  } else {
                    if (Number(value) >= (Number(this.formModel.laborcost) +
                      Number(this.formModel.interestexpense) +
                      Number(this.formModel.depreciationdepletion)) +
                      Number(this.formModel.assetimpairmentloss)) {
                      callback()
                    } else {
                      callback(new Error('应大于(其中：人工成本+利息支出+折旧折耗+资产减值损失)'))
                    }
                  }
                }
              }
            ],
            event: {
              change: () => { this.handleCalcTotalprofit() }
            }
          },
          {
            type: 'NumberInput',
            prop: 'laborcost',
            label: '其中：人工成本',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: (val) => { if (val) this.$refs['fourth_3'].$refs.simpleForm.validateField('totalcost') }
            }
          },
          {
            type: 'NumberInput',
            prop: 'interestexpense',
            label: '利息支出',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: (val) => { if (val) this.$refs['fourth_3'].$refs.simpleForm.validateField('totalcost') }
            }
          },
          {
            type: 'NumberInput',
            prop: 'depreciationdepletion',
            label: '折旧折耗',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: (val) => { if (val) this.$refs['fourth_3'].$refs.simpleForm.validateField('totalcost') }
            }
          },
          {
            type: 'NumberInput',
            prop: 'assetimpairmentloss',
            label: '资产减值损失',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: (val) => { if (val) this.$refs['fourth_3'].$refs.simpleForm.validateField('totalcost') }
            }
          }
        ],

        fourth_4: [
          { type: 'NumberInput', prop: 'totalprofit', label: '利润总额', viewFlag: true },
          { type: 'NumberInput', prop: 'investmentincome', label: '投资收益' },
          { type: 'NumberInput', prop: 'dividend', label: '分红' },
          { type: 'Input', prop: 'profitlossreason', label: '盈亏原因分析' }
        ],

        fifth_1: [
          { type: 'Title', title: '本年累计人员变动', col: { span: 24 } }
        ],

        fifth_2: [
          { type: 'NumberInput', prop: 'subtotalreduction', label: '减员小计', viewFlag: true },
          {
            type: 'NumberInput',
            prop: 'naturalreduction',
            label: '自然减员',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: () => {
                this.handleSumSubtotalreduction()
              }
            }
          },
          {
            type: 'NumberInput',
            prop: 'agreementcancellation',
            label: '协议解除劳动合同',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: () => {
                this.handleSumSubtotalreduction()
              }
            }
          },
          {
            type: 'NumberInput',
            prop: 'earlyretirement',
            label: '提前退休',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: () => {
                this.handleSumSubtotalreduction()
              }
            }
          },
          {
            type: 'NumberInput',
            prop: 'otherreduction',
            label: '其他',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: () => {
                this.handleSumSubtotalreduction()
              }
            }
          }
        ],
        fifth_3: [
          { type: 'NumberInput', prop: 'subtotalincrease', label: '增员小计', viewFlag: true },
          {
            type: 'NumberInput',
            prop: 'contractemployee',
            label: '合同化用工',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: () => {
                this.handleSumSubtotalincrease()
              }
            }
          },
          {
            type: 'NumberInput',
            prop: 'marketemployee',
            label: '市场化用工',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: () => {
                this.handleSumSubtotalincrease()
              }
            }
          },
          {
            type: 'NumberInput',
            prop: 'laboremployee',
            label: '劳务用工',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: () => {
                this.handleSumSubtotalincrease()
              }
            }
          },
          {
            type: 'NumberInput',
            prop: 'otherincrease',
            label: '其他',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true },
            event: {
              change: () => {
                this.handleSumSubtotalincrease()
              }
            }
          }
        ],
        fifth_4: [
          { type: 'Title', title: '在职人数', col: { span: 24 } },
          {
            type: 'NumberInput',
            prop: 'contractemployeecurrent',
            label: '合同化员工',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true }
          },
          {
            type: 'NumberInput',
            prop: 'marketemployeecurrent',
            label: '市场化用工',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true }
          },
          {
            type: 'NumberInput',
            prop: 'laboremployeecurrent',
            label: '劳务用工',
            rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true }
          }
        ],
        fifth_5: [
          { type: 'Title', title: '期末在职人数', col: { span: 24 } },
          { type: 'NumberInput', prop: 'currentemployeefinally', label: '期末在职人数', viewFlag: true }
        ]
      }
    }
  },
  props: {
    showFlag: { type: Boolean, default: false },
    viewFlag: { type: Boolean },
    detailData: { type: Object },
    extData: { type: Object }
  },
  methods: {
    handleDialogClose () {
      this.currentShowFlag = false
    },

    handleDataSubmit () {
      // 通过promise批量校验表单校验
      Promise.all(
        Object.keys(this.formField).map(key => {
          return this.$refs[key].validate()
        })
      )
        .then(resp => {
          let loadingFlag = this.$loading({ target: this.$el.querySelector('.el-dialog') })
          let submitModel = this.$_.cloneDeep(this.formModel)
          Object.assign(submitModel, { sys_mid: this.extData.sys_mid })
          this.$axios
            .post(this.detailData.id ? BASEURL.update : BASEURL.save, submitModel)
            .then(resp => {
              this.currentShowFlag = false
              this.$message({ type: 'success', message: this.$t('tip.saveSuccess') })
              this.handleDataSearch()
            })
            .finally(() => {
              loadingFlag.close()
            })
        })
        .catch(err => {
          console.log(err)
          this.activeName = err.tabName
        })
    },
    handleDialogOpen () {
      if (!this.detailData.id) {
        return
      }

      this.detailData.uploadquarter = this.detailData.uploadquarter + ''
      this.detailData.uploadyear = this.detailData.uploadyear + ''
      this.$set(this, 'formModel', this.detailData)
    },

    handleDialogClosed () {
      Object.keys(this.formField).forEach(key => {
        this.$refs[key].resetFields()
      })
      this.activeName = 'first'
      this.formModel = {}
    },

    // 计算在职人员情况->减员小计
    handleSumSubtotalreduction () {
      if (isNaN(Number(this.formModel.naturalreduction)) ||
        isNaN(Number(this.formModel.agreementcancellation)) ||
        isNaN(Number(this.formModel.earlyretirement)) ||
        isNaN(Number(this.formModel.otherreduction))) {
      } else {
        this.formModel.subtotalreduction = Number(this.formModel.naturalreduction) +
          Number(this.formModel.agreementcancellation) +
          Number(this.formModel.earlyretirement) +
          Number(this.formModel.otherreduction)
      }
    },

    // 计算在职人员情况->增员小计
    handleSumSubtotalincrease () {
      if (isNaN(Number(this.formModel.contractemployee)) ||
        isNaN(Number(this.formModel.marketemployee)) ||
        isNaN(Number(this.formModel.laboremployee)) ||
        isNaN(Number(this.formModel.otherincrease))) {
      } else {
        this.formModel.subtotalincrease = Number(this.formModel.contractemployee) +
          Number(this.formModel.marketemployee) +
          Number(this.formModel.laboremployee) +
          Number(this.formModel.otherincrease)
      }
    },

    // 计算资产负债情况->资产负债率
    handleCalcAssetsliabilities () {
      if (isNaN(Number(this.formModel.totalassets)) ||
        isNaN(Number(this.formModel.totalliabilities))) {
      } else {
        this.formModel.assetsliabilities = (Number(this.formModel.totalliabilities) /
          Number(this.formModel.totalassets))
      }
    },
    // 计算主要经营情况->利润总额
    handleCalcTotalprofit () {
      if (isNaN(Number(this.formModel.totalincome)) ||
        isNaN(Number(this.formModel.totalcost))) {
      } else {
        this.formModel.totalprofit = (Number(this.formModel.totalincome) -
          Number(this.formModel.totalcost))
      }
    }
  }
}
</script>
